#include "Estep.h"
#include <string.h>
#include <sys/stat.h>

int estep_theme_setup(Estep *es, const char *theme, const char *group);
const char *estep_theme_find(const char *name);

int
main (int argc, char ** argv)
{
	Estep *es;
	char *name = NULL;
	int i;

	if (!ecore_init())
	{
		fprintf(stderr, "ERROR: Can't initialize Ecore.\n");
		return 1;
	}

	if (!ecore_evas_init())
	{
		fprintf(stderr, "ERROR: Can't initialize Ecore_Evas.\n");
		return 1;
	}

	ecore_app_args_set(argc, (const char **) argv);

	es = calloc(1, sizeof(Estep));
	if (!es)
	{
		fprintf(stderr, "ERROR: Can't create main structure\n");
		return 1;
	}

	es->ee = ecore_evas_software_x11_new(0, 0, 0, 0, 255, 255);
	if (!es->ee)
	{
		fprintf(stderr, "ERROR: Can't create an Ecore_Evas.\n");
		return 1;
	}
	ecore_evas_title_set(es->ee, "Estep");
	ecore_evas_name_class_set(es->ee, "Estep", "Estep");

	edje_init();
	edje_frametime_set(1.0 / 30.0);

	es->evas = ecore_evas_get(es->ee);
	ecore_evas_callback_delete_request_set(es->ee, estep_cb_quit);
	ecore_evas_callback_destroy_set(es->ee, estep_cb_quit);
	ecore_event_handler_add(ECORE_EVENT_SIGNAL_EXIT, estep_cb_quit,
								NULL);
	ecore_evas_callback_mouse_in_set(es->ee, estep_cb_mouse_in);
	ecore_evas_callback_mouse_out_set(es->ee, estep_cb_mouse_out);
	ecore_evas_callback_resize_set(es->ee, estep_cb_configure);
	ecore_evas_borderless_set(es->ee, TRUE);
	ecore_evas_shaped_set(es->ee, TRUE);

	/* The edje object for gui */
	es->gui = edje_object_add(es->evas);
	evas_object_name_set(es->gui, "gui");
	evas_object_move(es->gui, 0, 0);
	evas_object_show(es->gui);

	/* Dragable object to make life easier */
	es->drag = esmart_draggies_new(es->ee);
	esmart_draggies_button_set(es->drag, 1);
	evas_object_layer_set(es->drag, -1);
	evas_object_move(es->drag, 0, 0);
	evas_object_name_set(es->drag, "drag");
	evas_object_show(es->drag);

	i = 0;
	while ((i < argc) && (argc))
	{
		if (!strcmp(argv[i], "--estep-theme"))
		{
			if (i + 1 < argc)
				name = strdup(argv[i + 1]);
		}
		i++;
	}

	if (!estep_theme_setup(es, name, "estep"))
	{
		fprintf(stderr, "ERROR: Setting theme failed somewhere.\n");
		return 1;
	}

	ecore_evas_show(es->ee);
	ecore_main_loop_begin();

	ecore_evas_shutdown();
	ecore_shutdown();
	edje_shutdown();
	FREE(name);
	FREE(es);

	return 0;
}

int
estep_theme_setup(Estep *es, const char *theme, const char *group)
{
	const char *name;
	Evas_Coord mw, mh;

	name = estep_theme_find(theme);

	if (!edje_object_file_set(es->gui, name, "estep"))
	{
		fprintf(stderr, "ERROR: Can't set theme to %s.\n", name);
		return 0;
	}

	edje_object_size_min_get(es->gui, &mw, &mh);
	ecore_evas_size_min_set(es->ee, mw, mh);
	if ((mw != 0) && (mh != 0))
	{
		evas_object_resize(es->gui, mw, mh);
		ecore_evas_resize(es->ee, mw, mh);
	}
	else
	{
		evas_object_resize(es->gui, 150, 150);
		ecore_evas_resize(es->ee, 150, 150);
	}

	edje_object_signal_callback_add(es->gui, "estep,up", "*",
			estep_cb_speed_up, es);
	edje_object_signal_callback_add(es->gui, "estep,down", "*",
			estep_cb_speed_down, es);
	edje_object_signal_callback_add(es->gui, "estep,forward", "*",
			estep_cb_dir_forwards, es);
	edje_object_signal_callback_add(es->gui, "estep,reverse", "*",
			estep_cb_dir_reverse, es);
	edje_object_signal_callback_add(es->gui, "estep,state", "*",
			estep_cb_change_state, es);

	return 1;
}

const char *
estep_theme_find(const char *name)
{

	static char ret[PATH_MAX];
	struct stat st;

	if (!name)
		name = DEFAULT_THEME;

	/* If given full path */
	if (!stat(name, &st))
		return name;

	/* Try installed dir */
	snprintf(ret, sizeof(ret), PACKAGE_DATA_DIR"/themes/%s.edj", name);

	if (!stat(name, &st))
		return ret;

	/* Try source directory */
	snprintf(ret, sizeof(ret),
		PACKAGE_SOURCE_DIR"/data/themes/%s/%s.edj", name, name);

	/* Only return NULL if finding default theme failed */
	if (!strcmp(name, DEFAULT_THEME))
		return stat(ret, &st) ? NULL : ret;
	else
		return estep_theme_find(DEFAULT_THEME);
}


